//
//  ZGAdressInfoView.m
//  NewBee
//
//  Created by leergou on 16/8/22.
//  Copyright © 2016年 WhiteHouse. All rights reserved.
//

#import "ZGAdressInfoView.h"
#import "ZGCustomKeyboard.h"
#import "ZGSendAddressModel.h"


#define kMargin 5
#define kHeight 40

@interface ZGAdressInfoView ()

/* telNumLabel */
@property (nonatomic,strong) UILabel *telNumLabel;

/* cityLabel */
@property (nonatomic,strong) UILabel *cityLabel;

/* zoneLabel */
@property (nonatomic,strong) UILabel *zoneLabel;

/* detailLabel */
@property (nonatomic,strong) UILabel *detailLabel;

/* leftButton */
@property (nonatomic,strong) UIButton *leftButton;

/* rightButton */
@property (nonatomic,strong) UIButton *rightButton;


@end

@implementation ZGAdressInfoView

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

#pragma mark - 点击事件 -
- (void)leftButtonClick:(UIButton *)sender{
    
    self.currentButton.selected = NO;
    
    sender.selected = YES;
    
    self.currentButton = sender;
    
    if (self.genderSelect) {
        self.genderSelect(sender.currentTitle);
    }
}

- (void)cancelItemClick{
    
    self.cityTextField.text = self.addressModel.city;
}

- (void)sureItemClick{
    
    [self.cityTextField resignFirstResponder];
}

#pragma mark - 重写模型 set 方法,分发数据 -
- (void)setAddressModel:(ZGSendAddressModel *)addressModel{
    _addressModel = addressModel;
    
    self.nameTextField.text = addressModel.name;
    self.telNumTextField.text = addressModel.telNum;
    self.cityTextField.text = addressModel.city;
    self.zoneTextField.text = addressModel.zone;
    self.detailTextField.text = addressModel.detailAddr;
    
    if ([addressModel.gender isEqualToString:@"先生"]) {
        self.leftButton.selected = YES;
        self.currentButton = self.leftButton;
    } else if ([addressModel.gender isEqualToString:@"女士"]){
        self.rightButton.selected = YES;
        self.currentButton = self.rightButton;

    }
    
}

- (void)setCityList:(NSArray *)cityList{
    _cityList = cityList;
    
    self.customKeyboard.cityList = cityList;
}


#pragma mark - setupUI -
- (void)setupUI{
    
    self.backgroundColor = [UIColor whiteColor];
    
    // 1.添加子控件
    [self addSubview:self.nameLabel];
    [self addSubview:self.nameTextField];
    
    [self addSubview:self.leftButton];
    [self addSubview:self.rightButton];
    
    [self addSubview:self.telNumLabel];
    [self addSubview:self.telNumTextField];
    
    [self addSubview:self.cityLabel];
    [self addSubview:self.cityTextField];
    
    [self addSubview:self.zoneLabel];
    [self addSubview:self.zoneTextField];
    
    [self addSubview:self.detailLabel];
    [self addSubview:self.detailTextField];
    
    // 2.添加约束
    [self.nameLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(kMargin);
        make.left.equalTo(2 * kMargin);
        make.width.equalTo(70);
        make.height.equalTo(kHeight);
    }];
    
    [self.nameTextField makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.nameLabel);
        make.leading.equalTo(self.nameLabel.trailing).offset(kMargin);
        make.width.equalTo(SCREEN_WIDTH * 0.7);
    }];
    
    [self.leftButton makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLabel.bottom);
        make.leading.equalTo(self.nameTextField);
        make.width.equalTo(80);
        make.height.equalTo(kHeight);
    }];
    
    [self.rightButton makeConstraints:^(MASConstraintMaker *make) {
        make.top.width.height.equalTo(self.leftButton);
        make.leading.equalTo(self.leftButton.trailing).offset(kMargin);
    }];
    
    [self.telNumLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.leftButton.bottom);
        make.leading.width.height.equalTo(self.nameLabel);
    }];
    
    [self.telNumTextField makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.top.equalTo(self.telNumLabel);
        make.leading.width.equalTo(self.nameTextField);
    }];
    
    [self.cityLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.telNumLabel.bottom);
        make.leading.width.height.equalTo(self.nameLabel);
    }];
    
    [self.cityTextField makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.top.equalTo(self.cityLabel);
        make.leading.width.equalTo(self.nameTextField);
    }];
    
    [self.zoneLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.cityLabel.bottom);
        make.leading.width.height.equalTo(self.nameLabel);
    }];
    
    [self.zoneTextField makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.top.equalTo(self.zoneLabel);
        make.leading.width.equalTo(self.nameTextField);
    }];
    
    [self.detailLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.zoneLabel.bottom);
        make.leading.width.height.equalTo(self.nameLabel);
    }];
    
    [self.detailTextField makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.top.equalTo(self.detailLabel);
        make.leading.width.equalTo(self.nameTextField);
    }];
    
    
    
}

#pragma mark - lazy -


- (UITextField *)detailTextField{
    if (!_detailTextField) {
        _detailTextField = [[UITextField alloc] init];
        _detailTextField.placeholder = @"详细地址";
    }
    return _detailTextField;
}

- (UILabel *)detailLabel{
    if (!_detailLabel) {
        _detailLabel = [[UILabel alloc] init];
        _detailLabel.text = @"详细地址";
    }
    return _detailLabel;
}

- (UITextField *)zoneTextField{
    if (!_zoneTextField) {
        _zoneTextField = [[UITextField alloc] init];
        _zoneTextField.placeholder = @"所在区域";
    }
    return _zoneTextField;
}

- (UILabel *)zoneLabel{
    if (!_zoneLabel) {
        _zoneLabel = [[UILabel alloc] init];
        _zoneLabel.text = @"所在地区";
    }
    return _zoneLabel;
}

- (UITextField *)cityTextField{
    if (!_cityTextField) {
        _cityTextField = [[UITextField alloc] init];
        _cityTextField.placeholder = @"城市";
        _cityTextField.inputView = self.customKeyboard;
        _cityTextField.inputAccessoryView = self.topBar;
    }
    return _cityTextField;
}

- (UILabel *)cityLabel{
    if (!_cityLabel) {
        _cityLabel = [[UILabel alloc] init];
        _cityLabel.text = @"所在城市";
    }
    return _cityLabel;
}

- (UITextField *)telNumTextField{
    if (!_telNumTextField) {
        _telNumTextField = [[UITextField alloc] init];
        _telNumTextField.placeholder = @"电话号码";
        _telNumTextField.keyboardType = UIKeyboardTypeNumberPad;
    }
    return _telNumTextField;
}

- (UILabel *)telNumLabel{
    if (!_telNumLabel) {
        _telNumLabel = [[UILabel alloc] init];
        _telNumLabel.text = @"手机号码";
    }
    return _telNumLabel;
}

- (UIButton *)rightButton{
    if (!_rightButton) {
         _rightButton = [self creatButtonWithTitle:@"女士" norImg:@"v2_noselected" selecImg:@"v2_selected"];
        
        [_rightButton addTarget:self action:@selector(leftButtonClick:) forControlEvents:UIControlEventTouchUpInside];

    }
    return _rightButton;
}

- (UIButton *)leftButton{
    if (!_leftButton) {
        
        _leftButton = [self creatButtonWithTitle:@"先生" norImg:@"v2_noselected" selecImg:@"v2_selected"];
        
        [_leftButton addTarget:self action:@selector(leftButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _leftButton;
}

- (UITextField *)nameTextField{
    
    if (!_nameTextField) {
        _nameTextField = [[UITextField alloc] init];
        _nameTextField.placeholder = @"联系人";
    }
    return _nameTextField;
}

- (UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.text = @"联系人";
    }
    return _nameLabel;
}

- (ZGCustomKeyboard *)customKeyboard{
    if (!_customKeyboard) {
        _customKeyboard = [[ZGCustomKeyboard alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
        
        __weak typeof(self) weakSelf = self;
        
        _customKeyboard.selectCity = ^(NSString *selectedCity){
            weakSelf.cityTextField.text = selectedCity;
            weakSelf.addressModel.city = selectedCity;
        };
    }
    return _customKeyboard;
}

- (UIToolbar *)topBar{
    if (!_topBar) {
        
        _topBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
        
        UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancelItemClick)];
        
        UIBarButtonItem *firstFlexble = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        
         UIBarButtonItem *selectLabel = [[UIBarButtonItem alloc] initWithTitle:@"选择城市" style:UIBarButtonItemStylePlain target:self action:nil];
        
        selectLabel.enabled = NO;
        
         UIBarButtonItem *secondFlexble = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        
         UIBarButtonItem *sureItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(sureItemClick)];
        
        _topBar.items = @[cancelItem, firstFlexble, selectLabel, secondFlexble, sureItem];
    }
    return _topBar;
}

#pragma mark - 自定义方法 -
- (UIButton *)creatButtonWithTitle:(NSString *)title norImg:(NSString *)norImg selecImg:(NSString *)selecImg{
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.contentMode = UIViewContentModeLeft;
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setTitle:title forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:norImg] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:selecImg] forState:UIControlStateSelected];
    
    return button;

}





@end




























